package com.cxh.mchat.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxh.mchat.demo.entity.Demo;
import com.cxh.mchat.demo.service.IDemoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxh
 * @since 2021-12-23
 */
@RestController
@RequestMapping("/demo/demo")
public class DemoController {
    @Resource
    private IDemoService demoService;

    @GetMapping
    public R<Object> query(Page<Demo> page){
        IPage<Demo> data = demoService.page(page, new QueryWrapper<Demo>().like("remark", "%%"));
        return R.ok(data);
    }

    @PostMapping
    public R<Object> add(@RequestBody @Validated Demo demo){
        return R.ok(demoService.save(demo));
    }

    @PutMapping
    public R<Object> update(@RequestBody @Validated Demo demo){
        return R.ok(demoService.updateById(demo));
    }

    @DeleteMapping
    public R<Object> delete(Long id){
        return R.ok(demoService.removeById(id));
    }
}
